/**
 * 
 */
package edu.umd.clip.lm.util;

import java.io.Serializable;
import java.util.Arrays;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class GrowingIntArray implements Serializable {
	private static final long serialVersionUID = 1L;

	private int array[];
	private int size;
	
	public GrowingIntArray(int array[], int size) {
		this.array = array;
		this.size = size;
	}
	
	public GrowingIntArray(int capacity) {
		this.array = new int[capacity];
		this.size = 0;
	}
	
	public void add(int val) {
		if (size >= array.length) {
			array = Arrays.copyOf(array, array.length << 1);
		}
		array[size] = val;
		++size;
	}

	public void setSize(int newSize) {
		array = Arrays.copyOf(array, newSize);
		if (size > newSize) size = newSize;
	}
	
	public int[] array() {
		return array;
	}
	
	public int size() {
		return size;
	}
	
	public void trim() {
		if (size < array.length)
			array = Arrays.copyOf(array, size);
	}

	@Override
	public String toString() {
		return "GrowingIntArray [array=" + Arrays.toString(array) + ", size="
				+ size + "]";
	}
}
